import java.util.Vector;
import java.util.Random;

//called by animal handler

//not moving, not mating, alive, food available
public class EatHandler {
	private static boolean DEBUG = false;
	public static void setDebug(boolean d) {DEBUG = d;}
	private static boolean DEBUGA = false;
	private static void setDebugA(boolean d) {DEBUGA = d;}
	private static boolean DEBUGB = false;
	private static void setDebugB(boolean d) {DEBUGB = d;}

	private static Vector<AnimalObject> herbivores;
	private static Vector<AnimalObject> meat;
	private static Vector<AnimalObject> carnivores;

	public static Vector<loc> shuffle(Vector<loc> old)
	{
		Random R = new Random();
		Vector<loc> N = new Vector<loc>();
		int S = old.size();
		for(int i=0; i<S;i++)
		{
			int rand = R.nextInt(old.size());
			N.add(old.get(rand));
			old.remove(rand);
		}
		return N;
	}

	public static void findFood(AnimalObject me){
		//System.out.println("Entered Handler"); //test
		herbivores = new Vector<AnimalObject>();
		meat = new Vector<AnimalObject>();
		carnivores = new Vector<AnimalObject>();

		if(!me.getType()) {
			//we eat plants
			if(DEBUG){
				System.out.println("I eat plants!");
				System.out.println("Energy: "+me.getEnergy());
			}
			if( PlantMap.PlantsHere(me.getLocation() ) == 0) {
				//search arealocs
				Vector<loc> bleh = me.getMyAreaLocs();
				bleh = shuffle(bleh);
				for(int i = 0; i < bleh.size();i++) {
					if(PlantMap.PlantsHere(bleh.get(i) ) >0) {
						MoveHandler.doMove(bleh.get(i), me);
						me.setMigrate(false);
						me.setDisposition(2);
						me.setEating(true);
						if(DEBUG)
							System.out.println("we found food and are moving");//test
						break;
					} else {
						me.setDisposition(0);
						me.setMigrate(true);
						me.setEating(false);
					}
				}
				if(DEBUG){
					System.out.println("we need to migrate"); //test
					System.out.println("Energy now:"+me.getEnergy()); //test
				}
			} else {
				PlantMap.eatPlantsHere(me.getLocation(), me.getMass()/3 );
				me.adjustEnergy(me.getMass());
				me.adjustHealth(1);
				if(me.getEnergy() > 100) {
					me.adjustMass(2);
					me.adjustHealth(20);
					me.setMigrate(false);
					me.setDisposition(0);
					me.setEating(true);
					if(DEBUG)
						System.out.println("we grew 10 :"+me.getMass());
				}
			}
		} else {
			//we eat meats
			if(DEBUGA) {
				System.out.println("I eat anything that moves");
				System.out.println("Energy :"+me.getEnergy());
				System.out.println(AnimalHandlerObject.getAnimalVector().size());
				System.out.println(meat.size());
				System.out.println(carnivores.size());
				System.out.println(herbivores.size());
			}
			Vector<loc> bleh = me.getMyAreaLocs();
			bleh = shuffle(bleh);

			if(!me.getChasing()) {
				for(int i = 0; i < bleh.size();i++) {
					for(int j = 0; j < AnimalHandlerObject.getAnimalVector().size(); j++) {
						if(!AnimalHandlerObject.getAnimalVector().get(j).getLiving())
							meat.add(AnimalHandlerObject.AnimalVector.get(j));
						if(!AnimalHandlerObject.getAnimalVector().get(j).getType())
							herbivores.add(AnimalHandlerObject.AnimalVector.get(j));
						else
							carnivores.add(AnimalHandlerObject.AnimalVector.get(j) );
					}
				}
				if(meat.size() > 0) {
					if(DEBUGB)
						System.out.println("MEAT");
					eatMeatBeSuccessful(me);
					me.setChasing(false);
				}else if (herbivores.size() > 0) {
					if(DEBUGB)
						System.out.println("HERBIVORE");
					tastyCritterIsTasty(me);
				} else if(carnivores.size() > 0) {
					if(DEBUGB)
						System.out.println("CANNIBAL");
					HannibalTheCannibal(me);
				} else {
					me.setMigrate(true);
					me.setDisposition(0);
				}
			} else {
				AttackDefenseHandler.doFight(me, me.getFocus());
			}
		}
	}

	public static void eatMeatBeSuccessful(AnimalObject me) {
		Random R = new Random();
		loc L = new loc(0,0);

		AnimalObject target = meat.get(R.nextInt(meat.size()));

		L = target.getLocation();
		me.setLocation(L);
		
		me.adjustHealth(5);
		me.setDisposition(2);
		me.setEating(true);
		target.adjustFoodValue(-10);
		me.adjustEnergy(40);
		if(me.getEnergy() >=75 ) {
			me.adjustMass(5);
			me.adjustHealth(20);
		}

		if(DEBUGB)
			System.out.println("Carrion! lets eat roadkill");

	}
	public static void tastyCritterIsTasty(AnimalObject me) {
		Random R = new Random();
		loc L = new loc(0,0);
		AnimalObject target = herbivores.get(R.nextInt(herbivores.size()));

		L = target.getLocation();
		me.setLocation(L);

		me.setFocus(target);
		target.setFocus(me);

		AttackDefenseHandler.doFight(me, target);
	}
	public static void HannibalTheCannibal(AnimalObject me) {
		Random R = new Random();
		loc L = new loc(0,0);
		AnimalObject target = carnivores.get(R.nextInt(carnivores.size()));

		L = target.getLocation();
		me.setLocation(L);

		me.setFocus(target);
		target.setFocus(me);

		AttackDefenseHandler.doFight(me, target);
	}
}
